package vn.nad.aoeclips.screen.detail.controller;

import java.net.MalformedURLException;
import java.util.ArrayList;

import vn.nad.android.aoeclips.R;
import vn.nad.aoeclips.common.model.entity.Article;
import vn.nad.aoeclips.common.utils.Constants;
import vn.nad.aoeclips.common.utils.YoutubeUtils;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;
import com.google.android.youtube.player.YouTubeBaseActivity;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayer.OnInitializedListener;
import com.google.android.youtube.player.YouTubePlayer.Provider;
import com.google.android.youtube.player.YouTubePlayerView;

/**
 * @author ducna6417
 */
public class DetailActivity extends YouTubeBaseActivity implements Constants,
		OnInitializedListener, OnClickListener {

	public static final String TAG = "DetailActivity";
	private ArrayList<Article> simpleArticles = new ArrayList<Article>();
	private int position;
	// list id video youtube
	private ArrayList<String> arrayVideoId = new ArrayList<String>();
	private YouTubePlayerView mYouTubePlayerView;

	private ImageView mImageViewBack;
	private ImageView mImageViewShare;

	// admob widget
	private AdView adView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_detail);

		findViewById();
		initData();
	}

	public void findViewById() {
		mYouTubePlayerView = (YouTubePlayerView) findViewById(R.id.youtube_view);

		mImageViewBack = (ImageView) findViewById(R.id.actionbar_left_icon);
		mImageViewBack.setOnClickListener(this);

		mImageViewShare = (ImageView) findViewById(R.id.actionbar_right_icon);
		mImageViewShare.setOnClickListener(this);

		// Create an ad.
		adView = (AdView) findViewById(R.id.adView);
		// Create an ad request. Check logcat output for the hashed device ID to
		// get test ads on a physical device.
		AdRequest adRequest = new AdRequest.Builder().build();

		// Start loading the ad in the background.
		adView.loadAd(adRequest);
		
		// Google analytics
		sendGoogleAnalytics("Detail", this);
	}

	public void initData() {

		Intent intent = getIntent();
		simpleArticles = intent.getParcelableArrayListExtra(ARGUMENT_ARTICLES);
		position = intent.getIntExtra(ARGUMENT_POSITION, 0);
		// get video id
		for (int i = 0; i < simpleArticles.size(); i++) {
			try {
				arrayVideoId.add(YoutubeUtils.extractYoutubeId(simpleArticles
						.get(i).getLink()));
			} catch (MalformedURLException e) {
				e.printStackTrace();
			}
		}
		mYouTubePlayerView.initialize(YOUTUBE_API_KEY, this);
	}

	@Override
	public void onInitializationFailure(Provider arg0,
			YouTubeInitializationResult arg1) {

	}

	@Override
	public void onInitializationSuccess(Provider provider,
			YouTubePlayer player, boolean wasRestored) {
		if (!wasRestored)
			player.loadVideos(arrayVideoId, position, 0);
	}

	@Override
	public void onClick(View v) {
		if (v == mImageViewBack) {
			finish();
		}

		if (v == mImageViewShare) {
		}
	}
	
	public  void sendGoogleAnalytics(String name, Activity activity) {
		// Get tracker.
		Tracker t = GoogleAnalytics.getInstance(this).newTracker(R.xml.app_tracker);

		// Set screen name.
		// Where path is a String representing the screen name.
		t.setScreenName(name);

		// Send a screen view.
		t.send(new HitBuilders.AppViewBuilder().build());
	}
}
